package com.JFapp.controller;

import com.JFapp.annotation.validator.group.register;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.entity.User;
import com.JFapp.domain.vo.PasswordVo;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.exception.SystemException;
import com.JFapp.service.LoginService;
import com.JFapp.service.UserService;
import com.JFapp.utils.JwtUtil;
import com.JFapp.utils.RedisCache;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@Api("前台登录管理")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserService userService;


    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(user);
    }


    @ApiOperation("重置密码")
    @PostMapping("/resetPassword")
    public ResponseResult resetPassword(@Param("email") String email){
        User user = userService.findUserByEmail(email);
        if (user==null){
            throw new RuntimeException(AppHttpCodeEnum.NOT_FIND_USER.getMessage());
        }
        return loginService.resetPassword(user);
    }

//    @ApiOperation("重置密码邮件")
//    @GetMapping("/changePassword")
//    public String ChangePasswordPage(@Param("token") String token) {
//        String id;
//        try {
//            Claims claims = JwtUtil.parseJWT(token);
//            String s = claims.getSubject();
//            id = claims.getId();
//            redisCache.setCacheObject("reset" + id, s);
//        } catch (Exception e) {
//            throw new RuntimeException(AppHttpCodeEnum.OUT_OF_TOKEN);
//        }
//        return "redirect:/changePassword/toChange?id=" + id;
//    }


    @ApiOperation("重置密码界面")
    @PostMapping("/changePassword/toChange")
    public ResponseResult change(@RequestBody PasswordVo password){
        if (Objects.equals(password.getPassword(), password.getNpassword())){
            return  loginService.changePassword( password.getPassword(),password.getResetToken());
        }else {
            throw new RuntimeException(AppHttpCodeEnum.CHECK_PASSWORD);
        }
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }


    @ApiOperation("注册")
    @PostMapping("/register")
    public ResponseResult register( @RequestBody @Validated(register.class) User user){
        return userService.register(user);
    }

}
